package com.lzh.financial.controller;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.lzh.financial.bean.PUserBean;
import com.lzh.financial.bean.PUserInfoBean;
import com.lzh.financial.service.IUserService;
import com.lzh.financial.util.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class WController {

    @Autowired
    private UserController userController;
    @Autowired
    private IUserService userService;

    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    /**
     * 登录跳转
     * @param username
     * @param password
     * @param captcha
     * @param deviceId
     * @return
     */
    @PostMapping(value = "/login")
    public String login(Model model,String username, String password, String captcha, String deviceId,HttpSession session) {
        JSONObject login = userController.login(username, password, captcha, deviceId,session);
        if (!login.get("status").toString().equals("FAIL")){
            return "main";
        }else {
            String data = login.get("status_desc").toString();
            model.addAttribute("msg", data);
            return "login";
        }
    }

    /**
     * 注册
     * @param userBean
     * @param userInfoBean
     * @return
     */
    @PostMapping(value = "/regist")
    public String login(Model model, PUserBean userBean, PUserInfoBean userInfoBean) {
        JSONObject regist = userController.regist(userBean,userInfoBean);
        if (!regist.get("status").toString().equals("FAIL")){
            model.addAttribute("regist", "注册成功");
            return "login";
        }else {
            String data = regist.get("status_desc").toString();
            model.addAttribute("msg", data);
            return "regist";
        }
    }

    @RequestMapping(value = "/toMain")
    public String toMain() {
            return "main";
    }

    @RequestMapping(value = "/toIndex")
    public String toIndex() {
        return "redirect:login.html";
    }

    @RequestMapping("/toRegist")
    public String toRegist() {
        return "redirect:regist.html";
    }

    @RequestMapping("/toDetail")
    public String toDetail() {
        return "people/details";
    }

    @RequestMapping("/toPeople")
    public String toPeople() {
        return "people/people";
    }

    @RequestMapping("/toMoney")
    public String toMoney() {
        return "people/money";
    }

    @RequestMapping("/toStatistcs")
    public String toStatistcs() {
        return "people/moneyStatistcs";
    }

    @RequestMapping("/toRoot")
    public String toRoot(HttpSession session) {
        PUserBean bean = userService.getById(jwtTokenUtil.getUserIdFromToken(session));
        if (!(ObjectUtil.isNotNull(bean) && bean.getRole() == 3)) {
            return "main";
        }
        return "root";
    }
}
